package com.runfast.waimai.web.controller;

import com.runfast.common.utils.TokenUtil;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.RunfastGoodsSell;
import com.runfast.waimai.dao.model.RunfastGoodsSellOption;
import com.runfast.waimai.dao.model.RunfastGoodsSellStandard;
import com.runfast.waimai.dao.model.RunfastGoodsSellType;
import com.runfast.waimai.service.RunfastGoodsSellOptionService;
import com.runfast.waimai.service.RunfastGoodsSellService;
import com.runfast.waimai.service.RunfastGoodsSellStandardService;
import com.runfast.waimai.service.RunfastGoodsSellTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品接口
 */
@RestController("userWmGoodsController")
@RequestMapping(value= "/api/user/wm/goods", headers = "token")
public class GoodsController {


    @Autowired
    private RunfastGoodsSellService goodsSellService;

    @Autowired
    private RunfastGoodsSellStandardService standardService;

    @Autowired
    private RunfastGoodsSellOptionService optionService;

    @Autowired
    private RunfastGoodsSellTypeService goodsSellTypeService;

    /**
     *
     * 搜索商家店内商品
     * @param businessId 商家id
     * @param pageable
     * @param token
     * @return
     */
    @PostMapping("/search")
    public Result<List<RunfastGoodsSell>> search(@RequestParam int businessId, @RequestParam String name, @PageableDefault Pageable pageable, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);


        List<RunfastGoodsSell> search = goodsSellService.search(name, businessId, pageable);
        return Result.ok("",search);
    }

    /**
     *
     * 获取商品下所有规格和属性列表
     * @param goodsId 商品id
     * @param token
     * @return
     */
    @PostMapping("/standardsAndattributes")
    public Result<RunfastGoodsSell> standardsAndattributes(@RequestParam int goodsId, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);


        return detail(goodsId,token);
    }


    /**
     *获取商品详情
     * @param goodsId
     * @param token
     * @return
     */
    @PostMapping("/detail")
    public Result<RunfastGoodsSell> detail(@RequestParam int goodsId, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);

        RunfastGoodsSell detail = goodsSellService.detail(goodsId);


        List<RunfastGoodsSellOption> goodsSellOptions = optionService.findOptionWithSub(goodsId);

        detail.setGoodsSellOptionList(goodsSellOptions);

        return Result.ok("",detail);
    }


    /**
     * 获取商家可用商品分类及分类下的商品信息
     * @param businessId 商家id
     * @param token
     * @return
     */
    @PostMapping("/catalogs")
    public Result<List<RunfastGoodsSellType>> list(@RequestParam int businessId, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);


        List<RunfastGoodsSellType> goodsSellTypeList = goodsSellTypeService.listWithGoods(businessId);
        return Result.ok("",goodsSellTypeList);
    }
}
